package com.iyao_app.mvp.signlist;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.iyao_app.R;
import com.iyao_app.adapter.SignListAdapter;
import com.iyao_app.base.BaseActivityWithHead;
import com.iyao_app.entry.PageInfo;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.StoryData;
import com.iyao_app.entry.StoryDetail;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyaowoqu.base_library.util.TimeUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/22.
 */

public class SignListActivity extends BaseActivityWithHead implements OnRefreshLoadmoreListener {
    @BindView(R.id.x_recyclerView)
    RecyclerView mXRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private int page = 1;
    private PageInfo pageInfo;
    private final int pageSize = 10;

    private SignListAdapter adapter;
    private String eventId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_list);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {

        eventId = getIntent().getStringExtra(Contants.TYPE);
        setTitleTV("签到列表");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter = new SignListAdapter(null);
        mXRecyclerView.setAdapter(adapter);
        mXRecyclerView.setLayoutManager(layoutManager);
        mRefreshLayout.setOnRefreshLoadmoreListener(this);
        mRefreshLayout.autoRefresh();
    }

    private boolean noMore = false;

    private void getData(final boolean isRefesh) {
        if (isRefesh) {
            page = 1;
        } else {
            page++;
        }
        NetUtils.getInstance().getEventList(null, eventId, "1", page, pageSize, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        StoryData storyData = model.getModel();
                        if (storyData != null) {
                            List<StoryDetail> list = storyData.list;
                            pageInfo = storyData.pageInfo;
                            if (pageInfo.pageTotal <= page) {
                                noMore = true;
                                mRefreshLayout.finishLoadmoreWithNoMoreData();
                            } else {
                                noMore = false;
                            }
                            if (isRefesh) {
                                mRefreshLayout.finishRefresh();
                            } else {
                                mRefreshLayout.finishLoadmore();
                            }
                            if (list == null || list.size() == 0) {
                                mRefreshLayout.finishLoadmoreWithNoMoreData();
                            } else {
                                if (isRefesh) {
                                    adapter.getData().clear();
                                }
                                dealData(list);
                            }
                        }
                    }

                }
            }

            @Override
            public void onFail(String message, int code) {
                if (isRefesh) {
                    mRefreshLayout.finishRefresh();
                } else {
                    mRefreshLayout.finishLoadmore();
                }
            }
        }, StoryData.class);
    }

    private void dealData(List<StoryDetail> list) {
        if (list.size() > 1) {
            for (int i = 0; i < list.size(); i++) {
                if (i > 0) {
                    StoryDetail temp = list.get(i - 1);
                    StoryDetail storyDetail = list.get(i);
                    boolean sameDay = TimeUtils.isSameDay(temp.createTime, storyDetail.createTime);
                    if (sameDay) {
                        storyDetail.isNotDayStart = true;
                    }
                }
            }
        }
        adapter.addData(list);
        if (noMore) {
            StoryDetail data = new StoryDetail();
            data.isLast = true;
            adapter.addData(data);
        }
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        getData(false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        getData(true);
    }
}
